package router

import (
	"iris-wms/apps/admin/web/dto/din"
	"iris-wms/apps/admin/web/dto/dout"
	"iris-wms/apps/admin/web/service"
	"iris-wms/common/pkg/db"
	"iris-wms/common/pkg/db/dao"
	"iris-wms/common/tools"
	"iris-wms/common/tools/result"

	"github.com/kataras/iris/v12"
)

func NewAdminLogRoute() *adminLogRoute {
	return &adminLogRoute{}
}

type adminLogRoute struct{}

func (r *adminLogRoute) Delete(ctx iris.Context, service_ service.AdminLogService) {
	var idList = make([]uint, 0)
	err := ctx.ReadJSON(&idList)
	if err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err = service_.DeleteByIdList(idList); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *adminLogRoute) Table(ctx iris.Context, service_ service.AdminLogService) {
	var in din.LogIn
	if err := ctx.ReadQuery(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	rows, total, err := service_.GetTable(in)
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}

	q := dao.Use(db.GDB1)
	userList, err := q.AdminUserOut.Find()
	if err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	policyList, err := q.AdminPolicyOut.Find()
	if err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	tools.VxeTableData[[]*dout.AdminLogOut](ctx, in.PageSize, in.CurrentPage, total, rows, iris.Map{
		"userList":   userList,
		"policyList": policyList,
	})
}
